using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Script.UI
{
    public class JieDaiChuUI : BaseUI
    {
        public IndentUI indentUI;
        public PingJiaUI pingJiaUI;
        public ClientUI clientUI;
        public Button enterZhuMianChuBtn;
        public Button nextClientBtn;
        public Text eatingDesc;
        public Button backBtn;

        private LaMianIndent _laMianIndent;
        private EPingJia _pingJia;
        private GameObject _mianGo;

        protected override void OnInit(params object[] args)
        {
            enterZhuMianChuBtn.onClick.AddListener(OnEnterZhuMianChuBtnClicked);
            nextClientBtn.onClick.AddListener(OnNextClientBtn);
            backBtn.onClick.AddListener(OnBackBtn);
        }

        protected override void OnDispose()
        {

        }

        protected override void OnOpen(params object[] args)
        {
            _laMianIndent = LaMianMgr.Instance.GetRandomClientDemand();

            if (args != null && args.Length > 0 && args[0] != null)
            {
                //厨房进入
                Debug.Log("厨房进入 接待处");
                _pingJia = (EPingJia)args[0];
                _mianGo = (GameObject)args[1];
                ShowPingJia();
            }
            else
            {
                //入口进入
                Debug.Log("入口进入 接待处");
                _pingJia = EPingJia.none;
                ShowIndent();
            }
        }

        protected override void OnClose()
        {

        }

        private void OnEnterZhuMianChuBtnClicked()
        {
            Close();
            UIMgr.Instance.OpenUI<ZhuMianMainView>(_laMianIndent);
        }

        public void ShowIndent()
        {
            pingJiaUI.Close();

            enterZhuMianChuBtn.gameObject.SetActive(true);
            nextClientBtn.gameObject.SetActive(false);
            eatingDesc.gameObject.SetActive(false);

            //打开订单界面
            indentUI.Open(_laMianIndent);

            //打开顾客界面
            ShowClient();
        }

        public void ShowPingJia()
        {
            //屏幕锁定
            enterZhuMianChuBtn.gameObject.SetActive(false);
            nextClientBtn.gameObject.SetActive(false);
            eatingDesc.gameObject.SetActive(false);

            //吃
            Debug.Log("吃");
            StartCoroutine(Eating(OnFinishEating));
        }

        private IEnumerator Eating(UnityAction cb)
        {
            Debug.Log("开始吃面");
            //显示吃饭中状态
            eatingDesc.gameObject.SetActive(true);
            var mg = Instantiate(_mianGo, eatingDesc.transform, false);
            mg.transform.localPosition = new Vector3(-160, 50, 0);
            
            yield return new WaitForSeconds(0.5f);
            mg.transform.Find("peicai").gameObject.SetActive(false);
            yield return new WaitForSeconds(0.5f);
            mg.transform.Find("tiaowei").gameObject.SetActive(false);
            yield return new WaitForSeconds(0.5f);
            mg.transform.Find("mian").gameObject.SetActive(false);
            yield return new WaitForSeconds(0.5f);
            mg.transform.Find("gaoTang").gameObject.SetActive(false);
            yield return new WaitForSeconds(1f);

            //结束显示吃饭中状态
            Destroy(mg);
            eatingDesc.gameObject.SetActive(false);

            cb?.Invoke();
            Debug.Log("结束吃面");
        }

        private void OnFinishEating()
        {
            indentUI.Close();

            //打开评价界面
            pingJiaUI.Open(_pingJia.ToString());

            switch (_pingJia)
            {
                case EPingJia.goushi:
                    //如果评价是最差顾客气走
                    //todo 顾客气走表现

                    break;
                case EPingJia.bad:
                    break;
                case EPingJia.good:
                    break;
                case EPingJia.best:
                    //如果评价是完美触发故事
                    Debug.Log($"time = {LaMianMgr.Instance.GetClientComeTimes(_laMianIndent.clientType)}");
                    if (LaMianMgr.Instance.GetClientComeTimes(_laMianIndent.clientType) > 1)
                    {
                        UIMgr.Instance.OpenUI<XiaoGuShi>(
                            _laMianIndent.clientType.ToString(),
                            Constent.XiaoGuShiDescDic[_laMianIndent.clientType]);
                    }
                    break;
                default:
                    break;
            }

            //解除屏幕锁定 显示下一个客人
            nextClientBtn.gameObject.SetActive(true);
        }

        public void ShowClient()
        {
            clientUI.Open(_laMianIndent.clientType.ToString(), _laMianIndent.clientType.ToString());
        }

        public void CloseClient()
        {
            clientUI.Close();
        }

        public void OnNextClientBtn()
        {
            ShowIndent();
        }

        public void OnBackBtn()
        {
            Close();
            UIMgr.Instance.CloseUI<ZhuMianMainView>();
            UIMgr.Instance.OpenUI<InitUI>();
            AudioMgr.Instance.PlayLoop("yu",1f);
        }
    }
}